﻿namespace Chain.Wechat
{
    using System;
    using System.Xml;

    public class ReqeustManager
    {
        public static IRequest GetRequest(string postData)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(postData);
            XmlNode root = doc.FirstChild;
            switch (root["MsgType"].InnerText)
            {
                case "text":
                    return new TextRequest(doc);

                case "image":
                    return new ImageRequest(doc);

                case "voice":
                    return new VoiceRequest(doc);

                case "video":
                    return new VideoRequest(doc);

                case "location":
                    return new LocationRequest(doc);

                case "link":
                    return new LinkRequest(doc);

                case "event":
                {
                    string eventType = root["Event"].InnerText;
                    if (((root["EventKey"] == null) || !(root["EventKey"].InnerText != "")) || (!(eventType == "subscribe") && !(eventType == "SCAN")))
                    {
                        switch (eventType)
                        {
                            case "subscribe":
                            case "unsubscribe":
                                return new EventRequest(doc);

                            case "LOCATION":
                                return new LocationEventRequest(doc);
                        }
                        if (!(eventType == "CLICK") && !(eventType == "VIEW"))
                        {
                            return new EventRequest(doc);
                        }
                        return new MenuRequest(doc);
                    }
                    return new ScanRequest(doc);
                }
            }
            return new IRequest(doc);
        }
    }
}

